﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Student
{
    public partial class UpdateInfo : Form
    {
        public UpdateInfo()
        {
            InitializeComponent();
        }
        //构造函数进行传值(点击修改之后把原先的信息显示在框内)
        public UpdateInfo(int id,string name,int stuId)
        {
            InitializeComponent();
            this.id = id;
            txt_Name.Text = name;
            txt_StuId.Text = stuId.ToString();
        }
        private int id;

        SqlConnection sqlcon = new SqlConnection(DbHelper.constr);

        private void UpdateInfo_Load(object sender, EventArgs e)
        {
            if (sqlcon.State == ConnectionState.Closed)
            {
                sqlcon.Open();
            }
        }
        //修改
        private void btn_OkUpdate_Click(object sender, EventArgs e)
        {
            //获取姓名
            string name = this.txt_Name.Text.Trim();
            //获取性别
            string sex = radioButton1.Checked ? "男" : "女";
            //获取学号
            string stuId = this.txt_StuId.Text.Trim();
            //姓名和学号不为空时
            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(stuId))
            {
                //修改的SQL语句
                string sql = string.Format("update StuInfo set Name='{0}',Sex='{1}',StuId={2} where Id={3}", name, sex, stuId, id);
                var result = DbHelper.GetIDU(sql);
                //返回信息
                if (result >= 1)
                {
                    MessageBox.Show("修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    StuInfoManagementSystem stu = new StuInfoManagementSystem();
                    stu.Show();
                }
                else
                {
                    MessageBox.Show("修改失败！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                this.Close();
            }
            //判断用户是否输入空值或空格
            else
            {
                if (string.IsNullOrEmpty(name) && string.IsNullOrEmpty(stuId))
                {
                    MessageBox.Show("姓名和学号不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (string.IsNullOrEmpty(name))
                {
                    MessageBox.Show("姓名不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (string.IsNullOrEmpty(stuId))
                {
                    MessageBox.Show("学号不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("别说了，我爱你！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        //退出
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
            StuInfoManagementSystem stu = new StuInfoManagementSystem();
            stu.Show();
        }
    }
}
